﻿
using DynamicViewFormApp.Entity;
using DynamicViewFormApp.Service;
using DynamicViewFormApp.Setting;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace DynamicViewFormApp.Util
{
    class DrawUtil
    {
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll ")]
        internal static extern bool BitBlt(
        IntPtr hdcDest, // handle to destination DC
        int nXDest, // x-coord of destination upper-left corner
        int nYDest, // y-coord of destination upper-left corner
        int nWidth, // width of destination rectangle
        int nHeight, // height of destination rectangle
        IntPtr hdcSrc, // handle to source DC
        int nXSrc, // x-coordinate of source upper-left corner
        int nYSrc, // y-coordinate of source upper-left corner
        System.Int32 dwRop // raster operation code
        );
        /// <summary>
        /// 绘制控件背景边框
        /// </summary>
        /// <param name="compent">控件对象</param>
        /// <param name="offset">内部偏移量</param>
        internal static void DrawSolidRectangle(Control compent, int offset)
        {
            Image image = new Bitmap(compent.Width, compent.Height);
            Graphics graphics = Graphics.FromImage(image);
            Pen pen = new Pen(Color.Black, 2f)
            {
                DashStyle = System.Drawing.Drawing2D.DashStyle.Solid
            };
            graphics.DrawRectangle(pen, offset, offset, compent.Width - (2 * offset), compent.Height - (2 * offset));
            //将位图贴到窗口上
            compent.BackgroundImage = image;
            //释放gdi和pen资源
            graphics.Dispose();
            pen.Dispose();
        }
        /// <summary>
        /// 绘制控件背景分栏边框
        /// </summary>
        /// <param name="component">控件对象</param>
        /// <param name="offset">内部偏移量</param>
        /// <param name="subfield">分栏数</param>
        internal static PageEntity DrawSolidRectangle(Control component, int offset, int subfield)
        {
            int width = component.Width;
            int height = component.Height;

            int subWidth = (width - ((subfield + 1) * offset)) / subfield;//计算分栏宽度
            int subHeight = height - (2 * offset);

            Image image = new Bitmap(component.Width, component.Height);
            Graphics graphics = Graphics.FromImage(image);
            Pen pen = new Pen(Color.Black, 2f)
            {
                DashStyle = System.Drawing.Drawing2D.DashStyle.Solid
            };
            List<PageEntity> positionList = new List<PageEntity>();
            for (int i = 0; i < subfield; i++)
            {
                graphics.DrawRectangle(pen, offset + (i * (subWidth + offset)), offset, subWidth, subHeight);
                int top = offset + offset / 2;
                int left = offset + (i * (subWidth + offset)) + offset / 2;

                positionList.Add(new PageEntity(subWidth, subHeight, top, left));
            }
            PageSetting.PageLocationList = positionList;
            //graphics.DrawRectangle(pen, offset, offset, compent.Width - (2 * offset), compent.Height - (2 * offset));
            //将位图贴到窗口上
            component.BackgroundImage = image;
            //释放gdi和pen资源
            graphics.Dispose();
            pen.Dispose();

            return new PageEntity(subWidth, subHeight, offset, offset);
        }

        /// <summary>
        /// Point是否在区域内
        /// </summary>
        /// <param name="top"></param>
        /// <param name="left"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="x">点坐标X</param>
        /// <param name="y">点坐标Y</param>
        /// <returns></returns>
        internal static bool IsPointInRect(int top, int left, int width, int height, int x, int y)
        {
            Point A = new Point(left, top);
            Point B = new Point(left + width, top);
            Point C = new Point(left + width, top + height);
            Point D = new Point(left, top + height);

            int a = (B.X - A.X) * (y - A.Y) - (B.Y - A.Y) * (x - A.X);
            int b = (C.X - B.X) * (y - B.Y) - (C.Y - B.Y) * (x - B.X);
            int c = (D.X - C.X) * (y - C.Y) - (D.Y - C.Y) * (x - C.X);
            int d = (A.X - D.X) * (y - D.Y) - (A.Y - D.Y) * (x - D.X);
            if ((a > 0 && b > 0 && c > 0 && d > 0) || (a < 0 && b < 0 && c < 0 && d < 0))
            {
                return true;
            }

            //      AB X AP = (b.x - a.x, b.y - a.y) x (p.x - a.x, p.y - a.y) = (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
            //      BC X BP = (c.x - b.x, c.y - b.y) x (p.x - b.x, p.y - b.y) = (c.x - b.x) * (p.y - b.y) - (c.y - b.y) * (p.x - b.x);
            return false;
        }

    }
}
